package com.yi.servicedriveruser.controller;

import com.yi.internalcommon.constant.DriverCarConstants;
import com.yi.internalcommon.dto.DriverUser;
import com.yi.internalcommon.dto.ResponseResult;
import com.yi.internalcommon.responese.DriverUserExistsResponse;
import com.yi.internalcommon.responese.OrderDriverResponse;
import com.yi.servicedriveruser.service.DriverUserService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private DriverUserService driverUserService;

    /**
     * 新增司机
     * @param driverUser
     * @return
     */
    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser){

        ResponseResult result = driverUserService.addDriverUser(driverUser);
        return result;
    }
    /**
     * 修改司机
     * @param driverUser
     * @return
     */
    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser){
        log.info(JSONObject.fromObject(driverUser).toString());
        ResponseResult result = driverUserService.updateDriverUser(driverUser);
        return result;
    }
    /**
     * 查询司机
     * @param driverPhone
     * @return
     */
    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistsResponse> checkDriver(@PathVariable("driverPhone") String driverPhone){
        log.info(driverPhone);
        ResponseResult<DriverUser> driverUserByPhone = driverUserService.getUserByPhone(driverPhone);
        DriverUser userByPhoneData = driverUserByPhone.getData();
        DriverUserExistsResponse driverUserExistsResponse=new DriverUserExistsResponse();
        int ifExists= DriverCarConstants.DRIVER_EXISTS;
        if(userByPhoneData == null){
        ifExists = DriverCarConstants.DRIVER_NOT_EXISTS;
            driverUserExistsResponse.setDriverPhone(driverPhone);
        }else{
            driverUserExistsResponse.setDriverPhone(userByPhoneData.getDriverPhone());
        }
        driverUserExistsResponse.setIfExists(ifExists);
        return ResponseResult.success(driverUserExistsResponse);
    }

    /**
     * 根据车辆id查找订单需要的司机信息
     * @param carId
     * @return
     */
    @GetMapping("/get-available-driver/{carId}")
    public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable("carId")Long carId){
    return driverUserService.getAvailableDriver(carId);
    }
}
